package com.example.cloud.action;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.example.cloud.biz.user.UserService;
import com.example.cloud.domain.common.ResultInfo;
import com.example.cloud.domain.model.user.UserDO;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * 实现API方法时，方法参数的注解必须保留
 *
 * @author tongyufu
 */
@RestController
@RequestMapping("/user")
public class UserAction {

    Logger              log = LoggerFactory.getLogger(getClass());
    @Autowired
    private UserService userService;

    /**
     * 查询
     *
     * @return 用户列表
     */
    @RequestMapping("search")
    public List<UserDO> search() {
        return null;
    }

    /**
     * RequestParam传参
     *
     * @param id 中文不会乱码
     * @return 用户
     */
    @ApiOperation(value = "查询指定ID用户", notes = "")
    @ApiImplicitParam(name = "id", value = "用户ID", paramType = "path", required = true, dataType = "Long")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @Transactional
    public UserDO getUserById(@PathVariable("id") Integer id) {
        return null;
    }

    /**
     * RequestHeader传参
     *
     * @param id id
     * @param name 中文有乱码
     * @return 用户
     */
    @ApiOperation("查询指定用户（通过header传参）")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String") })
    @RequestMapping(value = "header", method = RequestMethod.GET)
    public UserDO getUser(@RequestHeader("id") Integer id, @RequestHeader("name") String name) {
        log.info("---------@RequestHeader:" + name);
        return new UserDO(id, name);
    }

    /**
     * RequestBody传参
     *
     * @param userInfo 中文不会乱码
     * @return 用户
     */
    @ApiOperation("查询指定用户（通过RequestBody传参）")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "name", value = "用户姓名", required = true, dataType = "String") })
    @RequestMapping(value = "body", method = RequestMethod.POST)
    public UserDO getUser(@RequestBody UserDO userInfo) {
        log.info("---------@RequestBody:" + userInfo);
        return userInfo;
    }

    /**
     * 用户
     *
     * @param user 用户
     * @return 结果
     */
    @ApiOperation("新增用户")
    @ApiImplicitParam(name = "user", value = "用户信息", required = true, dataType = "UserVO")
    @RequestMapping(value = "save", method = RequestMethod.POST)
    public ResultInfo save(@RequestParam(name = "user", required = true) UserDO user) {
        return new ResultInfo(1, "新增用户成功");
    }
}
